using System;
using System.Collections;
using System.Net.Mail;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

namespace Jfc.DnnMarketingTools.MemberInvite
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The EditMemberInvite class is used to manage content
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// </history>
    /// -----------------------------------------------------------------------------
    partial class EditMemberInvite : PortalModuleBase
    {

    #region Private Members

        private int _ItemId = Null.NullInteger;

    #endregion

    #region Event Handlers

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            try
            {
                //Determine ItemId of MemberInvite to Update
                if(this.Request.QueryString["ItemId"] !=null)
                {
                    _ItemId = Int32.Parse(this.Request.QueryString["ItemId"]);
                }

                //If this is the first visit to the page, bind the role data to the datalist
                if (Page.IsPostBack == false)
                {
                    cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem") + "');");
                    ctlAudit.Attributes.Add("UserId", this.UserId.ToString());

                    // Hide InviteGuid row
                    lblInviteGuid.Visible = false;
                    txtInviteGuid.Visible = false;

                    if(_ItemId != -1)
                    {
                        //get content
                        MemberInviteController objMemberInvites = new MemberInviteController();
                        MemberInviteInfo objMemberInvite = objMemberInvites.GetMemberInvite(_ItemId,PortalId);
                        if (objMemberInvite != null)
                        {
                            txtInviteGuid.Text = objMemberInvite.InviteGuid.ToString();
                            txtFirstName.Text = objMemberInvite.FirstName;
                            txtLastName.Text = objMemberInvite.LastName;
                            txtEmail.Text = objMemberInvite.Email;
                            txtSubject.Text = objMemberInvite.Subject;
                            txtBody.Text = objMemberInvite.Body;

                            ctlStatus.Status = objMemberInvite.Status;
                            ctlStatus.StatusDate = objMemberInvite.StatusDate;
                            ctlStatus.EnableViewState = true;

                            ctlAudit.Attributes["UserId"] = objMemberInvite.CreatedByUser.ToString();
                            ctlAudit.CreatedByUser = objMemberInvite.CreatedByUser.ToString();
                            ctlAudit.CreatedDate = objMemberInvite.CreatedDate.ToString();
                        }
                        else
                        {
                            Response.Redirect(Globals.NavigateURL(), true);
                        }
                    }
                    else
                    {
                        MemberInviteSettingsController controller = new MemberInviteSettingsController();
                        MemberInviteSettingsInfo memberInviteSettings = controller.GetMemberInviteSettings(PortalId, UserId);
                
                        txtSubject.Text = memberInviteSettings.SubjectTemplate;
                        txtBody.Text = memberInviteSettings.BodyTemplate;

                        ctlStatus.Status = StatusType.Registered;

                        cmdDelete.Visible = false;
                        ctlStatus.Visible = false;
                        ctlAudit.Visible = false;
                    }
                }
           }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// cmdCancel_Click runs when the cancel button is clicked
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void cmdCancel_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// cmdDelete_Click runs when the delete button is clicked
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void cmdDelete_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                //Only attempt to delete the item if it exists already
                if (!Null.IsNull(_ItemId))
                {
                    MemberInviteController objMemberInvites = new MemberInviteController();
                    objMemberInvites.DeleteMemberInvite(_ItemId,PortalId);

                    //refresh cache
#pragma warning disable 0618
                    SynchronizeModule();
                    //ModuleController.SynchronizeModule(ModuleId);
#pragma warning restore
                }

                this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// cmdUpdate_Click runs when the update button is clicked
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void cmdUpdate_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                MemberInviteController objMemberInvites = new MemberInviteController();
                MemberInviteInfo objMemberInvite = new MemberInviteInfo();

                objMemberInvite.InviteId = _ItemId;
                objMemberInvite.PortalId = PortalId;
                if (!String.IsNullOrEmpty(txtInviteGuid.Text))
                    objMemberInvite.InviteGuid = new Guid(txtInviteGuid.Text);
                objMemberInvite.FirstName = txtFirstName.Text.Trim();
                objMemberInvite.LastName = txtLastName.Text.Trim();
                objMemberInvite.Email = txtEmail.Text.Trim();
                objMemberInvite.Subject = txtSubject.Text.Trim();
                objMemberInvite.Body = txtBody.Text;

                objMemberInvite.CreatedByUser = Int32.Parse(ctlAudit.Attributes["UserId"]);
                objMemberInvite.Status = ctlStatus.Status;
                objMemberInvite.StatusDate = ctlStatus.StatusDate;

                //Update the content within the MemberInvite table
                if(Null.IsNull(_ItemId))
                {
                    objMemberInvite.SetStatus(StatusType.Registered);
                    objMemberInvite.InviteGuid = Guid.NewGuid();
                    //objMemberInvites.Send(PortalId, objMemberInvite, PortalSettings, UserInfo);

                    objMemberInvites.AddMemberInvite(objMemberInvite);
                }
                else
                {
                    objMemberInvites.UpdateMemberInvite(objMemberInvite);
                }

                //refresh cache
#pragma warning disable 0618
                SynchronizeModule();
                //ModuleController.SynchronizeModule(ModuleId);
#pragma warning restore

                //Redirect back to the portal home page
                this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
             }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// cmdUpdate_Click runs when the update button is clicked
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void cmdSend_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                MemberInviteController objMemberInvites = new MemberInviteController();
                MemberInviteInfo objMemberInvite = new MemberInviteInfo();

                objMemberInvite.InviteId = _ItemId;
                objMemberInvite.PortalId = PortalId;
                if(!String.IsNullOrEmpty(txtInviteGuid.Text))
                    objMemberInvite.InviteGuid = new Guid(txtInviteGuid.Text);
                objMemberInvite.FirstName = txtFirstName.Text.Trim();
                objMemberInvite.LastName = txtLastName.Text.Trim();
                objMemberInvite.Email = txtEmail.Text.Trim();
                objMemberInvite.Subject = txtSubject.Text.Trim();
                objMemberInvite.Body = txtBody.Text;

                objMemberInvite.CreatedByUser = Int32.Parse(ctlAudit.Attributes["UserId"]);
                objMemberInvite.Status = ctlStatus.Status;
                objMemberInvite.StatusDate = ctlStatus.StatusDate;

                //Update and Send the content within the MemberInvite table
                ModuleController objModules = new ModuleController();
                Hashtable settings = objModules.GetTabModuleSettings(TabModuleId);

                SenderAddressType senderAddress = SenderAddressType.User;
                senderAddress = (SenderAddressType)Convert.ToInt32(Null.GetNull(settings["SenderAddress"], senderAddress));

                if (Null.IsNull(_ItemId))
                {
                    objMemberInvite.SetStatus(StatusType.Registered);
                    objMemberInvite.InviteGuid = Guid.NewGuid();

                    objMemberInvites.Send(objMemberInvite, senderAddress, PortalSettings, UserInfo);

                    objMemberInvites.AddMemberInvite(objMemberInvite);
                }
                else
                {
                    objMemberInvites.Send(objMemberInvite, senderAddress, PortalSettings, UserInfo);

                    objMemberInvites.UpdateMemberInvite(objMemberInvite);
                }

                //refresh cache
#pragma warning disable 0618
                SynchronizeModule();
                //ModuleController.SynchronizeModule(ModuleId);
#pragma warning restore

                //Redirect back to the portal home page
                this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

#endregion

    }
}

